

//
//  WYCBindingIDSideController.m
//  初试
//
//  Created by 李胜军 on 2017/10/23.
//  Copyright © 2017年 李胜军. All rights reserved.
//

#import "WYCBindingIDSideController.h"
#import "RSAUtil.h"
#import "WYCRealnameController.h"
#import "WYCbingSideModel.h"


@interface WYCBindingIDSideController ()
/** 模型*/
@property (strong , nonatomic) WYCbingSideModel *sidemodel;
/** 签发机关*/
@property(nonatomic,strong)UITextField *signingField;
/** 签发机关*/
@property(nonatomic,strong)UITextField *TermField;

/** 图片*/
@property (strong , nonatomic) UIImageView *iphoneimage;






@end

@implementation WYCBindingIDSideController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = WYCGlobalBg;
    
    
    [self setuprequster];
    //界面
    [self setupNav];
}
- (void)setuprequster{
    
    [self showLoadingWithMessage:@"扫描中..."];
    NSData * data = UIImageJPEGRepresentation(_imageStr, 1);
    NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"base64Img"] = encodedImageStr;
    params[@"side"] = @"2";
    [HttpTool POST:ocridcard params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        WYCLog(@"============%@",responseObject);
    if ([[responseObject objectForKey:@"code"] intValue] == 0) {
       [self hideLoading];
        NSDictionary * dict = [responseObject objectForKey:@"result"];
        WYCbingSideModel * bingsideModel = [[WYCbingSideModel alloc] init];
        [bingsideModel mj_setKeyValues:dict];
        self.sidemodel = bingsideModel;
//        [self.sidemodel mj_setKeyValues:dict];
        [self.iphoneimage sd_setImageWithURL:[NSURL URLWithString:dict[@"backUrl"]]];
        _signingField.text = dict[@"issue"];
        NSArray *stararray = [dict[@"startDate"] componentsSeparatedByString:@" "];
        NSArray *endarray = [dict[@"endDate"] componentsSeparatedByString:@" "];
        NSString * str = [NSString stringWithFormat:@"%@-%@",stararray[0],endarray[0]];
        _TermField.text = str;
        
        
    }else{
        
      [self hideLoading];
      [self showToast:msgStr];
     }
     } fail:^(NSURLSessionDataTask *task, NSError *error) {
         [self showToast:Failed];
        [self hideLoading];
}];
}
- (void)setupNav{
UIView * registerView = [[UIView alloc] init];
registerView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
registerView.backgroundColor = wycMain;
[self.view addSubview:registerView];


//不是Nav。所以自己写的View和label
UILabel * NavRigsterLabel = [[UILabel alloc] init];
NavRigsterLabel.frame = CGRectMake(ScreenWidth / 2 - 200 * px, titleY, 400 * px, 66 * px);
NavRigsterLabel.textColor = [UIColor whiteColor];
//    NavRigsterLabel.font = [UIFont systemFontOfSize:BigFont];
NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
NavRigsterLabel.text = @"身份证反面";
[registerView addSubview:NavRigsterLabel];



//返回按钮
UIButton *  backbutton = [[UIButton alloc] init];
[backbutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
backbutton.frame = CGRectMake(40 * px, titleY, 66 * px , 66 * px);
[registerView addSubview:backbutton];





//照片

    UIImageView * iphoneimage = [[UIImageView alloc] init];
//    iphoneimage.contentMode = UIViewContentModeScaleAspectFill;
    iphoneimage.contentMode = UIViewContentModeScaleAspectFit;
    iphoneimage.clipsToBounds=YES;//  是否剪切掉超出
    iphoneimage.frame = CGRectMake(40 * px, CGRectGetMaxY(registerView.frame) + 40 * px, ScreenWidth - 80 * px, 600 * px);
    [self.view addSubview:iphoneimage];
    self.iphoneimage = iphoneimage;
    
    
    //签发机关
    UILabel * signinglabel = [[UILabel alloc] init];
    signinglabel.text = @"签发机关：";
    signinglabel.font = [UIFont systemFontOfSize:BigMiddleFont];
    signinglabel.textColor = WYCRGBColor(120, 112, 115);
    signinglabel.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(iphoneimage.frame) + 80 * px, 320 * px, 60 * px);
    [self.view addSubview:signinglabel];
    
    //+1
    UITextField * signingField = [[UITextField alloc] init];
    signingField.frame = CGRectMake(CGRectGetMaxX(signinglabel.frame) + 20 * px, signinglabel.y, ScreenWidth, signinglabel.height);
    signingField.font = [UIFont systemFontOfSize:BigMiddleFont];
    signingField.text = self.signingstr;
    signingField.textColor = WYCRGBColor(120, 112, 115);
    [self.view addSubview:signingField];
    _signingField = signingField;
    
    
    //有效期
    UILabel * Termlabel = [[UILabel alloc] init];
    Termlabel.text = @"有限期:";
    Termlabel.font = [UIFont systemFontOfSize:BigMiddleFont];
    Termlabel.textColor = WYCRGBColor(120, 112, 115);
    Termlabel.frame = CGRectMake(iphoneimage.x, CGRectGetMaxY(signinglabel.frame) + 80 * px, 240 * px, 60 * px);
    [self.view addSubview:Termlabel];
    
    
    //性别
    UITextField * TermField = [[UITextField alloc] init];
    TermField.frame = CGRectMake(CGRectGetMaxX(Termlabel.frame) + 20 * px, Termlabel.y, ScreenWidth, Termlabel.height);
    TermField.font = [UIFont systemFontOfSize:BigMiddleFont];
    
    TermField.enabled  = NO;
//    TermField.text = str;
    TermField.textColor = WYCRGBColor(120, 112, 115);
    [self.view addSubview:TermField];
    _TermField = TermField;
    
    //登录按钮
    UIButton * loginButton = [[UIButton alloc] init];
    loginButton.frame = CGRectMake(Termlabel.x, CGRectGetMaxY(Termlabel.frame) + 80 * px, ScreenWidth - Termlabel.x * 2, 150 * px);
    loginButton.backgroundColor = wycMain;
    loginButton.layer.cornerRadius = 10 * px;
    [loginButton setTitle:@"确认保存" forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:72 * px];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
}

//保存操作
- (void)loginClick{
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    
    WYCLog(@"jsonStr-------%@----%@---%@----",self.sidemodel.startDate,self.sidemodel.endDate,self.sidemodel.backUrl);
    
    NSDictionary * jsonStr = @{@"userId":[WYCAccountTool unarchiveuserId],@"issue":self.signingField.text,@"startDate":self.sidemodel.startDate,@"endDate":self.sidemodel.endDate,@"backUrl":self.sidemodel.backUrl};
    
    NSError *error;
    
    NSData * data = [NSJSONSerialization dataWithJSONObject:jsonStr options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSString *pas = [RSAUtil encryptString:jsonString publicKey:RSA_P_KEY];
    
    params[@"ticket"] = [WYCAccountTool unarchiveticket];
    params[@"request"] = pas;
    [HttpTool POST:saveIdCardback params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([[responseObject objectForKey:@"code"] integerValue] == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"保存成功"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"idside" object:nil];
//            [self.navigationController popViewControllerAnimated:NO];
            UIViewController *vc =self.presentingViewController;
            
            //ReadBookController要跳转的界面
            
            while (![vc isKindOfClass:[WYCRealnameController class]]) {
                
                
                
                vc = vc.presentingViewController;
                
            }
            
            
            
            [vc dismissViewControllerAnimated:YES completion:nil];
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
    }];


    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_signingField resignFirstResponder];
//    [_passTexField resignFirstResponder]; 
}
//返回按钮
- (void)backClick{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
@end
